define(function(require, exports) {
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');

	// 活动编辑，单独出价广告位
	var Base = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				 max_list: 10
				,WhiteBlackType: 1
				,gridConfig: {
					 default_sort: false
					,auto_load: false
					,hasSearch: false
					,hasPager: false
					,hasTab: false
					,hasExport: false
					,hasAmount: false
					,opClick: false
					,list: {
						 scroll_type:"row"
						,scroll_size:10
					}
					,cols: [
						  {type: 'id'}
						 ,{name: 'PositionName', type:'index', text:LANG('广告位名称'), render:'renderSpot'}
						 ,{name: 'Domain', type:'index', text:LANG('媒体域名')}
						 ,{name: 'Size', type:'index', text:LANG('尺寸')}
						 ,{name: 'Price', text:LANG('出价'), render:'renderPriceSum', width:80}
						 ,{name: 'Stake', text:LANG('溢价'), format:'currency', render:'renderPriceEdit', width:100}
					]
					,operation: {
						width: 60,
						html: '<a href="#" data-op="remove">'+LANG("删除")+'</a>'
					}
				}
				,view_model: {
					// 删除所有按钮点击事件
					 evEmptyBtnClick: this.eventEmptyBtnClick
					// 导入按钮点击事件
					,evImportBtnClick: this.eventImportBtnClick
					// 新建按钮点击事件
					,evExportBtnClick: this.eventExportBtnClick
				}
			});

			this.$campaignPrice = 0;

			this.Super('init', arguments);
		}
		,afterBuild: function() {
			this.append([
				'<div class="button-wrap pr">',
					'<div class="pa" style="top: -55px;right: 0">',
						'<button ms-click="evEmptyBtnClick" class="btn btn-danger mr15">' + LANG('删除所有') + '</button>',
						'<button ms-click="evImportBtnClick" class="btn btn-primary mr15">' + LANG('导入') + '</button>',
						'<button ms-click="evExportBtnClick" class="btn btn-primary">' + LANG('导出') + '</button>',
					'</div>',
				'</div>',
				'<div pub-name="grid" pub-mod="@base/grid.base" class="grid-wrap" />'
			].join(''));

			// 导入导出窗口
			this.createAsync('dialog', 'modules/importExport.base');

			this.buildFromTemplate({
				grid: this.getConfig('gridConfig')
			});
		}
		,renderSpot: function(idx, val, row){
			if (!val){
				if (row.DomainId || row.SizeId){
					return LANG('通过“媒体域名+尺寸”改价的广告位');
				}else {
					return LANG('未知广告位');
				}
			}
			return val;
		}
		,renderPriceEdit: function(idx, val, row){
			return '￥<input type="text" class="P-campaignSpotPriceEdit" data-idx="'+idx+'" value="'+val+'"/>';
		}
		,renderPriceSum: function(idx, val, row){
			val = this.$campaignPrice + (val || 0);
			return require('modules/labels').format.formatCurrency(val);
		}
		,reset: function() {
			this.$data = [];
			this.$.grid.setData([]);
		}
		,getData: function() {
			var dom = this.find('.P-campaignSpotPriceEdit');
			util.each( this.$data,function(data, index) {
				data.Stake = dom[index].value || 0;
			});
			return this.$data;
		}
		,setData: function(data) {
			this.$data = data || [];
			this.$.grid.setData(this.$data);
			return this;
		}
		,importData: function() {
			this.$.dialog.setTitle(LANG('导入广告位出价')).setData('').show();
		}
		,exportData: function() {
			var formatData = JSON.stringify( this.$data );
			this.$.dialog.setTitle(LANG('导出广告位出价')).setData(formatData).show();
		}
		,emptyDataAll: function() {
			var self = this;
			pubjs.confirm(LANG('确认要清空当前所有出价记录吗?'), function(res){
				if (res){
					self.setData([]);
				}
			});
			return self;
		}
		// 删除广告位分组
		,removeGroup: function(index){
			var self = this;
			var list = self.getData();
			pubjs.confirm(LANG('要删除单独出价位: %1 吗?', list[index].Name || LANG('未知广告位')), function(isOk){
				if(isOk){
					list.splice(index, 1);
					self.setData(list);
				}
			});
			return self;
		}
		,onListOpClick: function(ev){
			var self = this;
			var param = ev.param;
			if (ev.source === self.$.grid){
				switch (param.op){
					case 'remove':
						self.removeGroup(param.index);
					break;
				}
			}
			return false;
		}
		,eventEmptyBtnClick: function() {
			this.emptyDataAll();
		}
		,eventImportBtnClick: function(ev) {
			this.importData();
		}
		,eventExportBtnClick: function(ev) {
			this.exportData();
		}
		,onImportList: function(ev) {
			var data;
			var list = this.getData();
			try {
				data = JSON.parse(ev.param);
			} catch (e){}

			if (util.isArray(data)) {
				list.push.apply(list, data);
				this.setData(list);
			}

			return false;
		}
	});

	exports.base = Base;
});